package com.gk.javaservice.util.test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class UnTitleFrame  extends JFrame implements ActionListener{

    Toolkit toolkit = Toolkit.getDefaultToolkit();

    // 设置窗体默认大小,使其适应屏幕大小
    private int DEFAULE_WIDTH = (int) (toolkit.getScreenSize().getWidth() * 0.4);
    private int DEFAULE_HEIGH = (int) (toolkit.getScreenSize().getHeight() * 0.5);

    // 设置窗体位置,使其在屏幕居中
    private int Location_x = (int) (toolkit.getScreenSize().getWidth() - DEFAULE_WIDTH) / 2;
    private int Location_y = (int) (toolkit.getScreenSize().getHeight() - DEFAULE_HEIGH) / 2;

    int mouseAtX = 0;
    int mouseAtY = 0;

    private JButton b2;

    public UnTitleFrame()
    {
        setUndecorated(true);//设置窗体的标题栏不可见

        /*
         * 设置窗体可拖动
         */

//        addMouseListener(new MouseAdapter()
//        {
//            public void mousePressed(MouseEvent e)
//            {
//                /*
//                 * 获取点击鼠标时的坐标
//                 */
//                mouseAtX = e.getPoint().x;
//                mouseAtY = e.getPoint().y;
//            }
//        });
//        addMouseMotionListener(new MouseMotionAdapter()
//        {
//            public void mouseDragged(MouseEvent e)
//            {
//                setLocation((e.getXOnScreen()-mouseAtX),(e.getYOnScreen()-mouseAtY));//设置拖拽后，窗口的位置
//            }
//        });

        /*
         * 实例化简单组件
         */
        JButton b1 = new JButton("最小化");
        b2 = new JButton("最大化");
        JButton b3 = new JButton("关闭");

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);

        b3.setBounds(DEFAULE_WIDTH-90,0,90,45);
        b2.setBounds(DEFAULE_WIDTH-185,0,90,45);
        b1.setBounds(DEFAULE_WIDTH-280,0,90,45);

        JLabel label = new JLabel("无边框窗体的演示",JLabel.CENTER);
        label.setBounds(280,280,450,45);
        label.setFont(new Font("华文新魏", Font.BOLD,40));

        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.GRAY);
        contentPane.setLayout(null);

        contentPane.add(b1);
        contentPane.add(b2);
        contentPane.add(b3);
        contentPane.add(label);


        setContentPane(contentPane);//设置ContentPane
        setSize(DEFAULE_WIDTH,DEFAULE_HEIGH);// 设置窗体默认大小,使其适应屏幕大小
        setLocation(Location_x, Location_y);//设置窗体在屏幕中的位置
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true); //设置窗体可见



    }

    public static void main(String[] args)
    {
        new UnTitleFrame();
    }


    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand() == "最大化")
        {
            setExtendedState(JFrame.MAXIMIZED_BOTH);
            b2.setText("正常化");
        }

        if(e.getActionCommand() == "正常化")
        {
            setExtendedState(JFrame.NORMAL);
            b2.setText("最大化");

        }

        if(e.getActionCommand() == "最小化")
        {
            setExtendedState(JFrame.ICONIFIED);
        }


        if(e.getActionCommand() == "关闭")
        {
            System.exit(0);
        }

    }

}
